<?php

/**
 * @copyright 2011 by Aaron, Brice, Dan, Mike.  This program is part
 * of Hapinoy Project, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version April 12, 2011
 *
 * @author Aaron Jarzombeck and Brice Rader
 */
/**
 * This is the database file.
 * It controls the store file to list all categories and
 * to add or delete a store into the database.
 * It is the only file that has the SQL queries and MySQL connections
 */
include_once('inc/functions.php');

class dbStore {

    private $db;

    static function createTable() {
        //create table store
        $query = "
        create table store(
            storeID      int             NOT NULL AUTO_INCREMENT,
            barangayID   int             NOT NULL,
            provinceID   int             NOT NULL,
            stateID      int             NOT NULL,
            storeName    varchar(45)     NOT NULL,
            constraint PK_Store PRIMARY KEY(storeID),
            constraint FK_Store_Barangay FOREIGN KEY(barangayID)
                references barangay (barangayID),
            constraint FK_Store_Province FOREIGN KEY(provinceID)
                references province (provinceID),
            constraint FK_Store_City FOREIGN KEY(cityID)
                references city (cityID)
            );
";
        connectToDatabase();
        $result = mysql_query($query);
        if ($result == false) {
            echo mysql_error();
            return false;
        }
        mysql_close();
        //Previous query was successful.
        return true;
    }

    /**
     * Gets all of the stores in the database
     * @return stores
     */
    function getAllStore() {
        $query = "SELECT * FROM store";
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close();
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return $result;
    }

    /**
     *
     * @param store $c
     * @return new store added to database
     */
    static function addStore($p) {
        if (!$p instanceof Store) {
            echo ("Invalid arguement insertInto ( ) function<br />");
            return false;
        }
        //Compose the INSERT INTO query srting. Primary keyvalue is missing.
        //Database server automatically generates a unique ID for the primary
        //key.
        $query = sprintf("INSERT INTO store VALUES (null, '%s')",
                        $c->getStoreName()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        printf("Store added has ID %d\n<br />", mysql_insert_id());
        mysql_close( );
        if ($result == false) {
            echo mysql_error ( );
            return false;
        }
        //query was successful.
        return true;
    }

    /**
     *
     * @param contact $c
     * @return checks to see what contacts are in the database
     */
    function checkStoreID($p) {
        if (!($p instanceof Store)) {
            return false;
        }
        $storeID = $p->getStoreID();
        connectToDatabase();
        if (mysql_num_rows(mysql_query("SELECT * FROM store WHERE
                storeID=$storeID")) == false) {
            mysql_close();
            //nothing existsmysql_num_rows
            return false;
        } else {
            return true;
        }
    }

    /**
     *
     * @param $storeID
     * @return ID
     */
    function getStoreID($storeID) {
        $p = new Store ();
        connectToDatabase();
        $result = mysql_query("SELECT * FROM store WHERE
                storeID=$storeID");
        mysql_close();
        if (!$result) {
            return false; //ID does not exist
        } else {
            $row = mysql_fetch_assoc($result);
            $p->setStoreID($storeID);
            return $p;
        }
    }

    /**
     *
     * @param $StoreName
     * @return Store
     */
    function getStoreName($storeName) {
        $p = new Store ();
        connectToDatabase();
        $result = mysql_query("SELECT * FROM store WHERE storeName=
                $storeName");
        mysql_close();
        if (!$result) {
            return false; //name does not exist
        } else {
            $row = mysql_fetch_assoc($result);
            $c->setStoreName($storeName);
            return $c;
        }
    }

    /**
     * Delete's store from database
     * @param contact $c
     * @return categories in database
     */
    function deleteStoreID($c) {
        if (!($c instanceof Store)) {
            echo ("Invalid argument deleteFrom( ) function<br />");
            return false;
        }

        //Compose the DELETE FROM query string.
        $query = sprintf("DELETE FROM store WHERE storeID = %d",
                        $c->getStoreID()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close( );
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return true;
    }

}
?>

